<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">

    </div>
    
    <script> 
let obj = {}
defineReactive(obj,"a",'1')
function defineReactive(obj,key,val) {
    Object.defineProperty(obj,key,{
        get () {
            console.log("调用get方法，val",val) 
            return val //直接返回 参数val 形成闭包
        },
        set (newVal) {
            if(val === newVal) {
                return
            }
            console.log("调用set方法，newVal",newVal)
            val = newVal
            updateFn()
        }
    })
}

let app = document.getElementById("app")
function updateFn() { 
    app.innerHTML = obj.a
}
setInterval(() => {
    obj.a = new Date().toLocaleTimeString();
}, 1000);
    </script>
</body>
</html>